package com.example.foo.api.web.member;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.api.boot.TokenHelper;
import com.example.foo.member.model.AdressManage;
import com.example.foo.member.query.AdressManageQuery;
import com.example.foo.member.service.AdressManageService;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

@org.springframework.web.bind.annotation.RestController
public class AdressManageAction {

    private final AdressManageService adressManageService;

    public AdressManageAction(AdressManageService adressManageService) {
        this.adressManageService = adressManageService;
    }

    @GetMapping("/api/adressManages")
    public AjaxResult index(AdressManageQuery adressManageQuery, Pageable pageable) {
        Long userId = TokenHelper.verifyToken();
        if (!Objects.equals(userId, adressManageQuery.getUid())) {
            return AjaxResult.error(403, "Access Forbidden");
        }
        PageResult<AdressManage> pageResult = adressManageService.findAdressManages(adressManageQuery, pageable);
        return AjaxResult.successData(pageResult);
    }

    @PostMapping("/api/adressManages")
    public AjaxResult createAdressManage(AdressManage adressManageForm) {
        Long userId = TokenHelper.verifyToken();
        
        AjaxResult validateError = validateForm(adressManageForm);
        if (validateError != null) {
            return validateError;
        }
        
        adressManageForm.setUid(userId);
        adressManageService.createAdressManage(adressManageForm);

        return AjaxResult.success();
    }

    @PutMapping("/api/adressManages/{id}")
    public AjaxResult updateAdressManage(@PathVariable("id") Long id, AdressManage adressManageForm) {
    	Long userId = TokenHelper.verifyToken();
        
        AjaxResult validateError = validateForm(adressManageForm);
        if (validateError != null) {
            return validateError;
        }

        if (!Objects.equals(userId, adressManageForm.getUid())) {
            return AjaxResult.error(403, "Access Forbidden");
        }
        
        AdressManage adressManage = adressManageService.findAdressManageById(id);
        if (adressManage == null) {
            return AjaxResult.error(404, "Not Found");
        }
        adressManageService.updateAdressManage(adressManageForm);

        return AjaxResult.success();
    }

    private AjaxResult validateForm(AdressManage adressManageForm) {
        if (StringUtils.isBlank(adressManageForm.getName())) {
            return AjaxResult.error(400, "收货人名称不能为空");
        }
        if (adressManageForm.getUid() == null) {
            return AjaxResult.error(400, "用户ID不能为空");
        }
        if (StringUtils.isBlank(adressManageForm.getCellphone())) {
            return AjaxResult.error(400, "收货人电话不能为空");
        }
        if (StringUtils.isBlank(adressManageForm.getProvince())) {
            return AjaxResult.error(400, "省不能为空");
        }
        if (StringUtils.isBlank(adressManageForm.getCity())) {
            return AjaxResult.error(400, "市不能为空");
        }
        if (StringUtils.isBlank(adressManageForm.getDistrict())) {
            return AjaxResult.error(400, "地区不能为空");
        }
        if (StringUtils.isBlank(adressManageForm.getAdressDetail())) {
            return AjaxResult.error(400, "详细地址不能为空");
        }
        if (StringUtils.isBlank(adressManageForm.getAreaCode())) {
            return AjaxResult.error(400, "邮政编码不能为空");
        }
        return null;
    }

}
